//
//  BrandView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/4.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "BrandView.h"
#import "BrandsContentCollectionViewCell.h"

#import "BrandViewController.h"

static NSString *brandsCellID = @"brandsCellID";

@interface BrandView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) UIView *headerV;
@property (strong, nonatomic) UIImageView *leftIV;
@property (strong, nonatomic) UIImageView *rightIV;
@property (strong, nonatomic) UILabel *headerTitle;
@property (strong, nonatomic) UILabel *desL;

@property (strong, nonatomic) UIView *contentV;
@property (strong, nonatomic) UICollectionView *brandsGoodsCollectionView;
@property (strong, nonatomic) UIView *loadMoreView;
@property (strong, nonatomic) UILabel *loadMoreL;
@property (strong, nonatomic) UIImageView *smallBackIV;

@end

@implementation BrandView

- (instancetype)init {
    
    self = [super init];
    
    if (self) {
        
        [self addSubview:self.headerV];
        [self.headerV addSubview:self.leftIV];
        [self.headerV addSubview:self.rightIV];
        [self.headerV addSubview:self.headerTitle];
        [self.headerV addSubview:self.desL];
        
        [self addSubview:self.contentV];
        [self.contentV addSubview:self.brandsGoodsCollectionView];
        [self.contentV addSubview:self.loadMoreView];
        [self.loadMoreView addSubview:self.loadMoreL];
        [self.loadMoreView addSubview:self.smallBackIV];
        
    }
    
    return self;
}


#pragma mark -更多品牌点击事件
- (void)loadMoreGestureClick:(UIGestureRecognizer *)recognizer {
    
    NSLog(@"更多品牌点击");

    BrandViewController *vc = [[BrandViewController alloc] init];
    [self.parentVC.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark -delegate、datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 5;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BrandsContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:brandsCellID forIndexPath:indexPath];
    
    
    cell.layer.borderColor = Colorf2f2f2.CGColor;
    cell.layer.borderWidth = jisuan(1);
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.supportType == 0) {
        
        ShoppingGoodsContentViewController *vc = [[ShoppingGoodsContentViewController alloc] init];
        [self.parentVC.navigationController pushViewController:vc animated:YES];
        
        NSLog(@"自营===品牌专区产品点击");
        
    }else {
        
        NSLog(@"非自营===品牌专区产品点击");
        
    }

}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    [self.headerV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.top.mas_equalTo(weakSelf.mas_top);
        make.height.mas_equalTo(jisuan(140));
    }];
    [self.headerTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.headerV.mas_centerX);
        make.top.mas_equalTo(weakSelf.headerV.mas_top).with.offset(jisuan(38));
        
    }];
    [self.leftIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.headerTitle.mas_centerY);
        make.right.mas_equalTo(weakSelf.headerTitle.mas_left).with.offset(-SmallMargin);
        make.width.mas_equalTo(jisuan(30));
        make.height.mas_equalTo(jisuan(24));
    }];
    [self.rightIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(weakSelf.headerTitle.mas_centerY);
        make.left.mas_equalTo(weakSelf.headerTitle.mas_right).with.offset(SmallMargin);
        make.width.mas_equalTo(jisuan(30));
        make.height.mas_equalTo(jisuan(24));
    }];
    [self.desL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.headerV.mas_centerX);
        make.bottom.mas_equalTo(weakSelf.headerV.mas_bottom).with.offset(-jisuan(30));
        
    }];
    
    [self.contentV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.mas_left);
        make.right.mas_equalTo(weakSelf.mas_right);
        make.top.mas_equalTo(weakSelf.headerV.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.mas_bottom);
        
    }];
    
    [self.brandsGoodsCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.contentV.mas_left).with.offset(Margin);
        make.right.mas_equalTo(weakSelf.contentV.mas_right).with.offset(-Margin);
        make.top.mas_equalTo(weakSelf.contentV.mas_top);
//        make.height.mas_equalTo(jisuan(290));
        make.bottom.mas_equalTo(weakSelf.contentV.mas_bottom).with.offset(-jisuan(60));

    }];
    [self.loadMoreView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentV.mas_left);
        make.right.mas_equalTo(weakSelf.contentV.mas_right);
        make.top.mas_equalTo(weakSelf.brandsGoodsCollectionView.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.contentV.mas_bottom);
        
//        make.height.mas_equalTo(jisuanH(60));
        
    }];
    [self.loadMoreL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.loadMoreView.mas_centerX).with.offset(-jisuan(10));
        make.centerY.mas_equalTo(weakSelf.loadMoreView.mas_centerY);
    }];
    
    [self.smallBackIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.loadMoreL.mas_right).with.offset(jisuan(10));
        make.centerY.mas_equalTo(weakSelf.loadMoreView.mas_centerY);
        make.width.mas_equalTo(jisuan(11));
        make.height.mas_equalTo(jisuan(20));
        
    }];
    
}

#pragma mark -lazy
- (UIView *)headerV {
    
    if (!_headerV) {
        
        _headerV = [[UIView alloc] init];
        _headerV.backgroundColor = WhiteColor;
    }
    
    return _headerV;
    
}

- (UIImageView *)leftIV {
    
    if (!_leftIV) {
        
        _leftIV = [[UIImageView alloc] init];
        _leftIV.contentMode = UIViewContentModeScaleAspectFit;
        _leftIV.image = [UIImage imageNamed:@"sectionHeaderLeft"];
    }
    
    return _leftIV;
}

- (UIImageView *)rightIV {
    
    if (!_rightIV) {
        
        _rightIV = [[UIImageView alloc] init];
        _rightIV.contentMode = UIViewContentModeScaleAspectFit;
        _rightIV.image = [UIImage imageNamed:@"sectionHeaderRight"];
        
    }
    
    return _rightIV;
}

- (UILabel *)headerTitle {
    
    if (!_headerTitle) {
        
        _headerTitle = [[UILabel alloc] init];
        _headerTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:jisuan(30)];
        _headerTitle.textAlignment = NSTextAlignmentCenter;
        _headerTitle.textColor = Color333333;
        _headerTitle.text = @"品牌专区";
    }
    
    return _headerTitle;
}

- (UILabel *)desL {
    
    if (!_desL) {
        
        _desL = [[UILabel alloc] init];
        _desL.textAlignment = NSTextAlignmentCenter;
        _desL.textColor = Color999999;
        _desL.font = Font(20);
        _desL.text = @"大牌不一定都很贵";
        
    }
    
    return _desL;
    
}

- (UIView *)contentV {
    
    if (!_contentV) {
        
        _contentV = [[UIView alloc] init];
        
    }
    
    return _contentV;
    
}

- (UICollectionView *)brandsGoodsCollectionView {
    
    if (!_brandsGoodsCollectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(jisuan(170), jisuan(289));
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        flowLayout.minimumLineSpacing = (WIDTH - 4 * jisuan(170) - 2 * Margin) / 3;
        flowLayout.minimumInteritemSpacing = jisuan(1);
        
        _brandsGoodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _brandsGoodsCollectionView.backgroundColor = WhiteColor;
        
        _brandsGoodsCollectionView.delegate = self;
        _brandsGoodsCollectionView.dataSource = self;
        
        _brandsGoodsCollectionView.showsVerticalScrollIndicator = NO;
        _brandsGoodsCollectionView.showsHorizontalScrollIndicator = NO;
        
        [_brandsGoodsCollectionView registerClass:[BrandsContentCollectionViewCell class] forCellWithReuseIdentifier:brandsCellID];
        
        if (@available (iOS 11.0, *)) {
            
            _brandsGoodsCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
    }
    
    return _brandsGoodsCollectionView;
    
}

- (UIView *)loadMoreView {
    
    if (!_loadMoreView) {
        
        _loadMoreView = [[UIView alloc] init];
        _loadMoreView.backgroundColor = WhiteColor;
        
    }
    
    return _loadMoreView;
    
}

- (UIImageView *)smallBackIV {
    
    if (!_smallBackIV) {
        
        _smallBackIV = [[UIImageView alloc] init];
        _smallBackIV.image = [UIImage imageNamed:@"smallBackBg1"];
        _smallBackIV.contentMode = UIViewContentModeScaleAspectFit;
        
    }
    
    return _smallBackIV;
}

- (UILabel *)loadMoreL {
    
    if (!_loadMoreL) {
        
        _loadMoreL = [[UILabel alloc] init];
        _loadMoreL.backgroundColor = WhiteColor;
        
        _loadMoreL.text = @"更多商品";
        _loadMoreL.textColor = Color1f89ff;
        _loadMoreL.font = Font(20);
        _loadMoreL.textAlignment = NSTextAlignmentRight;
        
        _loadMoreL.userInteractionEnabled = YES;
        UITapGestureRecognizer *loadMoreGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadMoreGestureClick:)];
        [_loadMoreL addGestureRecognizer:loadMoreGesture];
        
    }
    
    return _loadMoreL;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
